package com.lxr0860.lxr0860production_test.itemactivity;

import android.app.Activity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.lxr0860.lxr0860production_test.utils.MyUtils;
import com.lxr0860.lxr0860production_test.utils.Utils;
import com.ndk.demo.jni;
import com.lxr0860.lxr0860production_test.R;
import com.lxr0860.lxr0860production_test.utils.PsamControl;


public class ICC_0860Activity extends Activity {

    private TextView recTextView;
    private byte[] mDir;
    private byte[] mGene;
    private byte[] mDataIn;
    private byte mKeyID;
    private Button mPowerBtn;
    private jni jni;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.com);
        init_view();
        initData();
        initEvent();
        System.out.println("SerialPortMain-------------start");
    }
    private void initData() {

    }

    private void initEvent() {

        jni = new jni();

    }


    private void init_view() {
        // TODO Auto-generated method stub
        mPowerBtn = findViewById(R.id.power);
        Button mReSetBtn = findViewById(R.id.button_reSet);
        Button mGetRandomNumBtn = findViewById(R.id.GetRandomNumButton);
        Button mEncryBtn = findViewById(R.id.button_encry);
        Button mDecrBtn = findViewById(R.id.button_decr);
        RadioGroup mGroupCon = findViewById(R.id.GroupID);
        Button cleanBtn = findViewById(R.id.button_clean);
        //Button checkBtn = findViewById(R.id.button_check);
        Button test_All = findViewById(R.id.button_FunAll);

        mPowerBtn.setOnClickListener(new onclick());
        mReSetBtn.setOnClickListener(new onclick());
        mGetRandomNumBtn.setOnClickListener(new onclick());
        mDecrBtn.setOnClickListener(new onclick());
        mEncryBtn.setOnClickListener(new onclick());
        cleanBtn.setOnClickListener(new onclick());
        //checkBtn.setOnClickListener(new onclick());
        test_All.setOnClickListener(new onclick());
        //选择文件
        mDir = new byte[2];
        mDir[0] = 0x02;
        mDir[1] = 0x00;

        //DES初始化
        mGene = new byte[8];
        mGene[0] = (byte) 0xe0;
        mGene[1] = 0x04;
        mGene[2] = 0x00;
        mGene[3] = 0x00;
        mGene[4] = (byte) 0xfb;
        mGene[5] = 0x12;
        mGene[6] = 0x0b;
        mGene[7] = 0x09;


        //DES计算
        mDataIn = new byte[16];
        mDataIn[0] = (byte) 0x88;
        mDataIn[1] = (byte) 0x88;
        mDataIn[2] = (byte) 0x88;
        mDataIn[3] = (byte) 0x88;
        mDataIn[4] = (byte) 0x99;
        mDataIn[5] = (byte) 0x99;
        mDataIn[6] = (byte) 0x99;
        mDataIn[7] = (byte) 0x99;
        mDataIn[8] = (byte) 0x88;
        mDataIn[9] = (byte) 0x88;
        mDataIn[10] = (byte) 0x88;
        mDataIn[11] = (byte) 0x88;
        mDataIn[12] = (byte) 0x99;
        mDataIn[13] = (byte) 0x99;
        mDataIn[14] = (byte) 0x99;
        mDataIn[15] = (byte) 0x99;

        //keyID
        mKeyID = 0x02;

        //选择大小卡
        mGroupCon.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int cheackedBtn) {
                if (cheackedBtn == R.id.button_card1) {
                    Log.d("cheackedBtn", "小卡");
                    PsamControl.ICC_select_sock(1);
                    recTextView.append("已选择小卡\n");
                } else {
                    recTextView.append("已选择大卡\n");
                    Log.d("cheackedBtn", "大卡");
                    PsamControl.ICC_select_sock(2);
                }
            }
        });

        recTextView = (TextView) findViewById(R.id.recTextView);
        recTextView.setMovementMethod(ScrollingMovementMethod.getInstance());
        recTextView.setSelected(true);

    }

    private void scallToEnd() {
        int offset = recTextView.getLineCount() * recTextView.getLineHeight();
        if (offset > recTextView.getHeight()) {
            recTextView.scrollTo(0, offset - recTextView.getHeight());
        }
    }

    private boolean isONPower = false;

    class onclick implements OnClickListener {
        boolean isON = false;

        public onclick() {
            super();
        }

        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.button_clean) {
                recTextView.setText("");
                recTextView.scrollTo(0, 0);
            }else if (v.getId() == R.id.power){
                if (isON) {
                    //下电
                    if (PsamControl.ICC_interface_power(0) == 0) {
                        recTextView.append("下电成功\n");
                        mPowerBtn.setText("上电");
                        isON = false;
                        isONPower = isON;
                    } else {
                        recTextView.append("下电失败\n");
                    }

                } else {
                    //上电
                    if (PsamControl.ICC_interface_power(1) == 0) {
                        recTextView.append("上电成功\n");
                        mPowerBtn.setText("下电");
                        isON = true;
                        isONPower = isON;
                    } else {
                        recTextView.append("上电失败\n");
                    }
                }
                scallToEnd();
            }else if (v.getId() == R.id.button_reSet){
//                PsamControl.ICC_cpucard_ATR(new PsamControl.ICCTPDUDataListener() {
//                    @Override
//                    public void ICCResponseDataCallback(int status, String data, int dataLen, String statusCode) {
//                        if (status == 0) {
//                            recTextView.append("复位成功\n" + "data:" + data + "\ndataLen:" + dataLen + "\nstatusCode:" + statusCode + "\n");
//                        } else {
//                            recTextView.append("复位失败：" + status + "\n");
//                        }
//                    }
//                });
                byte[] ATR = new byte[128];
                byte[] ATRLEN = new byte[128];
                byte[] Card_ApplyInfo = new byte[256];
                int iccCpucardAtr = jni.ICC_cpucard_ATR(ATR,ATRLEN, (short) 1,(short)0,(short)17,(short)0);
                Log.e("ICC_cpucard_ATR",iccCpucardAtr+"");
                Log.e("ATR", Utils.bytesToString(ATR));
                Log.e("ATRLEN", Utils.bytesToString(ATRLEN));

                scallToEnd();
            }else if (v.getId() == R.id.GetRandomNumButton){
                /*PsamControl.ICC_T0_TPDU_RandomData(new PsamControl.ICCTPDUDataListener() {
                    @Override
                    public void ICCResponseDataCallback(int status, String data, int dataLen, String statusCode) {
                        if (status == 0) {
                            recTextView.append("取随机数成功\n" + "data:" + data + "\ndataLen:" + dataLen + "\nstatusCode:" + statusCode + "\n");
                        } else {
                            recTextView.append("取随机数失败：" + status + "\n");
                        }
                    }
                });*/
                byte[] databuff = new byte[128];
                short[] psw = new short[8];
                int iccToTpdu = jni.ICC_T0_TPDU((short) 1, (short) 0x84, (short) 0x00, (short) 0x00, (short) 0x00, (short) 0x08, databuff, psw);
//                int iccToTpdu = jni.ICC_TO_TPDU();
//                int iccToTpdu = jni.ICC_TO_TPDU(databuff, psw);
                Log.e("ICC_TO_TPDU",iccToTpdu+"");
                Log.e("databuf", MyUtils.hex2DebugHexString(databuff));
                Log.e("psw", psw[0]+""+psw[1]);

                scallToEnd();
            }else if (v.getId() == R.id.button_encry){
                PsamControl.ICC_TO_TPDU_Encry(mDir, mKeyID, mGene, mGene.length, mDataIn, mDataIn.length, new PsamControl.ICCTPDUDataListener() {
                    @Override
                    public void ICCResponseDataCallback(int status, String data, int dataLen, String statusCode) {
                        if (status == 0) {
                            recTextView.append("加密成功\n" + "data:" + data + "\ndataLen:" + dataLen + "\nstatusCode:" + statusCode + "\n");
                        } else {
                            recTextView.append("加密失败：" + status + "\n");
                        }
                    }
                });
                scallToEnd();
            }else if (v.getId() == R.id.button_decr){

                PsamControl.ICC_TO_TPDU_Decr(mDir, mKeyID, mGene, mGene.length, mDataIn, mDataIn.length, new PsamControl.ICCTPDUDataListener() {
                    @Override
                    public void ICCResponseDataCallback(int status, String data, int dataLen, String statusCode) {
                        if (status == 0) {
                            recTextView.append("解密成功\n" + "data:" + data + "\ndataLen:" + dataLen + "\nstatusCode:" + statusCode + "\n");
                        } else {
                            recTextView.append("解密失败：" + status + "\n");
                        }
                    }
                });
                scallToEnd();
            }
        }
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        if (isONPower) {
            PsamControl.ICC_interface_power(0);
        }
    }
}